import socket
import threading
import random

my_list = ["董卓", "袁绍", "曹操", "刘备"]

def funcA(new_tcp, address):
    print(f"{address}用户已连接")
    new_tcp.send(my_list[random.randint(0, len(my_list) - 1)].encode())
    data = new_tcp.recv(1024)
    if data:
        print(data.decode())
    else:
        print(f"{address}已断开连接")
        new_tcp.close()




def main():
    tcp = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    tcp.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    tcp.bind(('',8080))
    tcp.listen(128)

    while True:
        new_tcp, address = tcp.accept()
        threading.Thread(target=funcA, args=(new_tcp,address)).start()

if __name__ == '__main__':
    main()




